package io.github.talelin.latticy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import io.github.talelin.autoconfigure.exception.NotFoundException;
import io.github.talelin.latticy.bo.BannerWithItemsBO;
import io.github.talelin.latticy.dto.BannerDTO;
import io.github.talelin.latticy.mapper.BannerItemMapper;
import io.github.talelin.latticy.model.Banner;
import io.github.talelin.latticy.mapper.BannerMapper;
import io.github.talelin.latticy.model.BannerItem;
import io.github.talelin.latticy.service.BannerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tanrentao
 * @since 2021-08-10
 */
@Service
public class BannerServiceImpl extends ServiceImpl<BannerMapper, Banner> implements BannerService {

    @Autowired
    private BannerItemMapper bannerItemMapper;

    @Override
    public void delete(Integer id) {
        Banner banner = this.getById(id);
        if (banner == null) {
            throw new NotFoundException(20000);
        }
        this.getBaseMapper().deleteById(id);
    }

    @Override
    public void update(BannerDTO dto, Integer id) {
        Banner banner = this.getById(id);
        if (banner == null) {
            throw new NotFoundException(20000);
        }
        BeanUtils.copyProperties(dto, banner);
        this.updateById(banner);
    }

    @Override
    public BannerWithItemsBO getWithItems(Integer id) {
        Banner banner = this.getById(id);
        if (banner == null) {
            throw new NotFoundException(20000);
        }

        // LambdaQueryWrapper<BannerItem> wrapper = new QueryWrapper<BannerItem>().lambda();
        // wrapper.eq("banner_id", id);

        // LambdaQueryWrapper<BannerItem> wrapper = new LambdaQueryWrapper<>();
        // wrapper.eq(BannerItem::getBannerId, id);
        // List<BannerItem> bannerItems = bannerItemMapper.selectList(wrapper);

        List<BannerItem> bannerItems = new LambdaQueryChainWrapper<>(bannerItemMapper)
                .eq(BannerItem::getBannerId, id)
                .list();

        return new BannerWithItemsBO(banner, bannerItems);
    }
}
